package com.geek.water.service.impl;

/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/8/11
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geek.water.constant.WorkOrderStatus;
import com.geek.water.entity.WorkOrder;
import com.geek.water.mapper.WorkOrderMapper;
import com.geek.water.service.WorkOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 工单服务实现类
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class WorkOrderServiceImpl implements WorkOrderService {

    private final WorkOrderMapper workOrderMapper;

    @Override
    public boolean createWorkOrder(WorkOrder workOrder) {
        try {
            // 设置默认状态为待处理
            workOrder.setStatus(WorkOrderStatus.PENDING);
            // 设置创建时间
            workOrder.setCreatedAt(LocalDateTime.now());

            return workOrderMapper.insert(workOrder) > 0;
        } catch (Exception e) {
            log.error("创建工单失败", e);
            return false;
        }
    }

    @Override
    public Page<WorkOrder> findPage(Integer currentPage, Integer pageSize, String type, String status, Long deviceId, Long siteId, Long creatorId, Long assigneeId) {
        try {
            Page<WorkOrder> page = new Page<>(currentPage, pageSize);

            LambdaQueryWrapper<WorkOrder> wrapper = new LambdaQueryWrapper<>();

            // 添加查询条件
            if (type != null && !type.isEmpty()) {
                wrapper.eq(WorkOrder::getType, type);
            }
            if (status != null && !status.isEmpty()) {
                wrapper.eq(WorkOrder::getStatus, status);
            }
            if (deviceId != null) {
                wrapper.eq(WorkOrder::getDeviceId, deviceId);
            }
            if (siteId != null) {
                wrapper.eq(WorkOrder::getSiteId, siteId);
            }
            if (creatorId != null) {
                wrapper.eq(WorkOrder::getCreatorId, creatorId);
            }
            if (assigneeId != null) {
                wrapper.eq(WorkOrder::getAssigneeId, assigneeId);
            }

            // 按创建时间倒序排列
            wrapper.orderByDesc(WorkOrder::getCreatedAt);

            return workOrderMapper.selectPage(page, wrapper);
        } catch (Exception e) {
            log.error("分页查询工单失败", e);
            return null;
        }
    }

    @Override
    public WorkOrder getById(Long id) {
        try {
            return workOrderMapper.selectById(id);
        } catch (Exception e) {
            log.error("根据ID查询工单失败", e);
            return null;
        }
    }



    @Override
    public boolean assignWorkOrder(Long workOrderId, Long assigneeId) {
        try {
            LambdaUpdateWrapper<WorkOrder> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(WorkOrder::getId, workOrderId)
                    .set(WorkOrder::getAssigneeId, assigneeId)
                    .set(WorkOrder::getStatus, WorkOrderStatus.ASSIGNED);

            return workOrderMapper.update(null, wrapper) > 0;
        } catch (Exception e) {
            log.error("派发工单失败", e);
            return false;
        }
    }

    @Override
    public boolean updateWorkOrderStatus(Long workOrderId, String status) {
        try {
            LambdaUpdateWrapper<WorkOrder> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(WorkOrder::getId, workOrderId)
                    .set(WorkOrder::getStatus, status);

            return workOrderMapper.update(null, wrapper) > 0;
        } catch (Exception e) {
            log.error("更新工单状态失败", e);
            return false;
        }
    }

    @Override
    public boolean completeWorkOrder(Long workOrderId, String description) {
        try {
            LambdaUpdateWrapper<WorkOrder> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(WorkOrder::getId, workOrderId)
                    .set(WorkOrder::getStatus, WorkOrderStatus.COMPLETED)
                    .set(WorkOrder::getFinishedAt, LocalDateTime.now());

            // 如果有完成描述，更新描述字段
            if (StringUtils.hasText(description)) {
                wrapper.set(WorkOrder::getDescription, description);
            }

            return workOrderMapper.update(null, wrapper) > 0;
        } catch (Exception e) {
            log.error("完成工单失败", e);
            return false;
        }
    }

    @Override
    public boolean evaluateWorkOrder(Long workOrderId, String evaluation) {
        try {
            LambdaUpdateWrapper<WorkOrder> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(WorkOrder::getId, workOrderId)
                    .set(WorkOrder::getEvaluation, evaluation);

            return workOrderMapper.update(null, wrapper) > 0;
        } catch (Exception e) {
            log.error("评价工单失败", e);
            return false;
        }
    }

    @Override
    public List<WorkOrder> findByDeviceId(Long deviceId) {
        try {
            LambdaQueryWrapper<WorkOrder> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(WorkOrder::getDeviceId, deviceId)
                    .orderByDesc(WorkOrder::getCreatedAt);

            return workOrderMapper.selectList(wrapper);
        } catch (Exception e) {
            log.error("根据设备ID查询工单失败", e);
            return null;
        }
    }

    @Override
    public List<WorkOrder> findBySiteId(Long siteId) {
        try {
            LambdaQueryWrapper<WorkOrder> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(WorkOrder::getSiteId, siteId)
                    .orderByDesc(WorkOrder::getCreatedAt);

            return workOrderMapper.selectList(wrapper);
        } catch (Exception e) {
            log.error("根据站点ID查询工单失败", e);
            return null;
        }
    }


}
